require "Common/define"
require "Logic/Config"
require "Common/functions"

RedDotsAssistantSkillHandler ={};
local this = RedDotsAssistantSkillHandler;

local lastSkillInfo = {}
local assistantskillConfig = nil;
local redState = Config.RedDotsFlagState.None
function RedDotsAssistantSkillHandler.init()
    CloseEvent(Config.EventType.LevUpAssistantSkill,this.update);
    CloseEvent(Config.EventType.Fresh_Currency,this.update);
    ListenEvent(Config.EventType.Fresh_Currency,this.update);
    ListenEvent(Config.EventType.LevUpAssistantSkill,this.update);

    if assistantskillConfig==nil then
        assistantskillConfig = G.dataTable["t_assistantskill"];
    end

    return this;
end
function RedDotsAssistantSkillHandler.getFlag(...)
    --每天只提示一次
    --if GetPlayerPrefsTodayFlag("todayShowAssistantSkill") then
    --    redState = Config.RedDotsFlagState.None
    --else
        redState = this.CheckRed()
    --end
    return redState
end
function RedDotsAssistantSkillHandler.HasRedPoint()
    return redState
end
function RedDotsAssistantSkillHandler.RedTipToday()
    SetPlayerPrefsTodayFlag("todayShowAssistantSkill",true)
    this.update()
end
--参数1：指定技能组，nil代表检查技能组
---女武神援助技页签红点
function RedDotsAssistantSkillHandler.CheckRed()
    if not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.SupSkillCultivate,false) then
        return Config.RedDotsFlagState.None
    end
    local skillInfo = logicMgr.SkillManager.getSupSkillInfo();
    local skillGroup = logicMgr.SkillManager.getSupSkillConfig();
    if table.getn(lastSkillInfo) ==0 and table.getn(skillInfo)>0  then
        --解锁女武神
    lastSkillInfo = skillInfo;
        return Config.RedDotsFlagState.Needful;
    end
    lastSkillInfo = skillInfo;

    local skillId = nil --...;
    local skillList = {};
    if skillId ~= nil then
        table.insert(skillList, skillId);
    else
        for k, v in pairs(skillGroup) do
            table.insert(skillList, k);
        end
    end
    local curGroup;
    local curData;
    local isUpgrade;
    for i = 1, #skillList do
        curGroup = skillGroup[tonumber(skillList[i])];
        curData = this.findSkillInfo(tonumber(skillList[i]), skillInfo);
        if curData ~= nil and #curGroup >= curData.skillLev + 1 then
            isUpgrade = true;
            for k, v in pairs(curGroup[curData.skillLev].f_Cost) do
                if next(v) == nil then
                    isUpgrade = false;
                    break;
                end
                if v[2] > logicMgr.ItemManager.getCurrencyCountByModelId(v[1]) then
                    isUpgrade = false;
                    break;
                end
            end
            if isUpgrade then
                return Config.RedDotsFlagState.Needful;
            end
        end
    end
    return Config.RedDotsFlagState.None;
end

function RedDotsAssistantSkillHandler.findSkillInfo(id, data)
    for k, v in pairs(data) do
        if assistantskillConfig[tostring(v.skillId)].f_AssistantSkillGroup == id then
            return v;
        end
    end
    return nil;
end

function RedDotsAssistantSkillHandler.update()
    RedDotsManager.addBroadcast(Config.RedDotsType.AssistantSkill);
end